<?php
namespace app\seller\controller;
use app\seller\Controller;
use pidan\facade\Db;
use app\seller\model\Seller;
use pidan\captcha\facade\Captcha;

class Index extends Controller
{

	public function cache(){
		//取的范围  lang mall type
		$region=input('request.region','','trim');
		$app=$this->app;

		$all=[];
		//语言包
		if(empty($region) || $region=='lang') {
			$lang=$app->make('lang');
			$file='lang/' . $lang->getLangSet() . '.php';
			$lang->load([base_path().$file,app_path().$file],'getLoad');//一个公用，一个当前应用
			$all['lang']=$lang->get(null,[],'getLoad');
		}
		//用户信息  登陆后获取
		if(token_session('?user_id')){
			//平台数据
			if(empty($region) || $region=='mall') $all['mall']=Db::name('mall')->cache('mall')->column('name,logo','id');
			//平台任务类型
			if(empty($region) || $region=='type') $all['type']=Db::name('type')->cache('type')->column('mall_id,name','id');
			//用户信息
			if(empty($region) || $region=='user') $all['user']=['email'=>token_session('user_email'),'nickname'=>token_session('user_nickname')];
			//店铺信息
			if(empty($region) || $region=='store') $all['store']=Db::name('store')->where('user_id',token_session('user_id'))->column('mall_id,name','id');
		}

		return $this->api($all);
	}
	public function menu(){
		$menu=[
			[
				'title'=>'商家中心',
				'icon'=>'layui-icon-home',
				'jump'=>'system/about'
			],
			[
				'title'=>'发布推广任务',
				'icon'=>'layui-icon-survey',
				'jump'=>'task/add'
			],
			[
				'title'=>'任务管理',
				'icon'=>'layui-icon-template',
				'jump'=>'system/about'
			],
			[
				'title'=>'资金明细',
				'icon'=>'layui-icon-list',
				'jump'=>'system/about'
			],
			[
				'title'=>'店铺管理',
				'icon'=>'layui-icon-website',
				'jump'=>'store/list'
			],
			[
				'title'=>'申诉中心',
				'icon'=>'layui-icon-tips',
				'jump'=>'system/about'
			],
			[
				'title'=>'推广赚金',
				'icon'=>'layui-icon-rmb',
				'jump'=>'system/about'
			],
			[
				'title'=>'黑名单',
				'icon'=>'layui-icon-group',
				'jump'=>'system/about'
			],
				array (
					'title' => '主页',
					'icon' => 'layui-icon-home',
					'list' =>
						array (
							0 =>
								array (
									'title' => '控制台',
									'jump' => '/',
								),
							1 =>
								array (
									'name' => 'homepage1',
									'title' => '主页一',
									'jump' => 'home/homepage1',
								),
							2 =>
								array (
									'name' => 'homepage2',
									'title' => '主页二',
									'jump' => 'home/homepage2',
								),
						),
				),
				array (
					'name' => 'component',
					'title' => '组件',
					'icon' => 'layui-icon-component',
					'list' =>
						array (
							0 =>
								array (
									'name' => 'grid',
									'title' => '栅格',
									'list' =>
										array (
											0 =>
												array (
													'name' => 'list',
													'title' => '等比例列表排列',
												),
											1 =>
												array (
													'name' => 'mobile',
													'title' => '按移动端排列',
												),
											2 =>
												array (
													'name' => 'mobile-pc',
													'title' => '移动桌面端组合',
												),
											3 =>
												array (
													'name' => 'all',
													'title' => '全端复杂组合',
												),
											4 =>
												array (
													'name' => 'stack',
													'title' => '低于桌面堆叠排列',
												),
											5 =>
												array (
													'name' => 'speed-dial',
													'title' => '九宫格',
												),
										),
								),
							1 =>
								array (
									'name' => 'button',
									'title' => '按钮',
								),
							2 =>
								array (
									'name' => 'form',
									'title' => '表单',
									'list' =>
										array (
											0 =>
												array (
													'name' => 'element',
													'title' => '表单元素',
												),
											1 =>
												array (
													'name' => 'group',
													'title' => '表单组合',
												),
										),
								),
							3 =>
								array (
									'name' => 'nav',
									'title' => '导航',
								),
							4 =>
								array (
									'name' => 'tabs',
									'title' => '选项卡',
								),
							5 =>
								array (
									'name' => 'progress',
									'title' => '进度条',
								),
							6 =>
								array (
									'name' => 'panel',
									'title' => '面板',
								),
							7 =>
								array (
									'name' => 'badge',
									'title' => '徽章',
								),
							8 =>
								array (
									'name' => 'timeline',
									'title' => '时间线',
								),
							9 =>
								array (
									'name' => 'anim',
									'title' => '动画',
								),
							10 =>
								array (
									'name' => 'auxiliar',
									'title' => '辅助',
								),
							11 =>
								array (
									'name' => 'layer',
									'title' => '通用弹层',
									'list' =>
										array (
											0 =>
												array (
													'name' => 'list',
													'title' => '功能演示',
												),
											1 =>
												array (
													'name' => 'special-demo',
													'title' => '特殊示例',
												),
											2 =>
												array (
													'name' => 'theme',
													'title' => '风格定制',
												),
										),
								),
							12 =>
								array (
									'name' => 'laydate',
									'title' => '日期时间',
									'list' =>
										array (
											0 =>
												array (
													'name' => 'demo1',
													'title' => '功能演示一',
												),
											1 =>
												array (
													'name' => 'demo2',
													'title' => '功能演示二',
												),
											2 =>
												array (
													'name' => 'theme',
													'title' => '设定主题',
												),
											3 =>
												array (
													'name' => 'special-demo',
													'title' => '特殊示例',
												),
										),
								),
							13 =>
								array (
									'name' => 'table-static',
									'title' => '静态表格',
									'jump' => 'component/table/static',
								),
							14 =>
								array (
									'name' => 'table',
									'title' => '表格组件',
									'list' =>
										array (
											0 =>
												array (
													'title' => '表格综合演示',
												),
											1 =>
												array (
													'name' => 'auto',
													'title' => '列宽自动分配',
												),
											2 =>
												array (
													'name' => 'data',
													'title' => '赋值已知数据',
												),
											3 =>
												array (
													'name' => 'tostatic',
													'title' => '转化静态表格',
												),
											4 =>
												array (
													'name' => 'resetPage',
													'title' => '自定义分页',
												),
											5 =>
												array (
													'name' => 'radio',
													'title' => '开启单选框',
												),
											6 =>
												array (
													'name' => 'cellEdit',
													'title' => '双击单元格编辑',
												),
											7 =>
												array (
													'name' => 'form',
													'title' => '加入表单元素',
												),
											8 =>
												array (
													'name' => 'style',
													'title' => '设置单元格样式',
												),
											9 =>
												array (
													'name' => 'fixed',
													'title' => '固定列',
												),
											10 =>
												array (
													'name' => 'parseData',
													'title' => '解析任意数据格式',
												),
											11 =>
												array (
													'name' => 'onrow',
													'title' => '行事件',
												),
											12 =>
												array (
													'name' => 'search',
													'title' => '外部搜索',
												),
											13 =>
												array (
													'name' => 'initSort',
													'title' => '设置初始排序',
												),
											14 =>
												array (
													'name' => 'cellEvent',
													'title' => '单元格事件',
												),
											15 =>
												array (
													'name' => 'thead',
													'title' => '复杂表头',
												),
										),
								),
							15 =>
								array (
									'name' => 'laypage',
									'title' => '分页',
									'list' =>
										array (
											0 =>
												array (
													'name' => 'demo1',
													'title' => '功能演示一',
												),
											1 =>
												array (
													'name' => 'demo2',
													'title' => '功能演示二',
												),
										),
								),
							16 =>
								array (
									'name' => 'dropdown',
									'title' => '下拉菜单',
								),
							17 =>
								array (
									'name' => 'upload',
									'title' => '上传',
									'list' =>
										array (
											0 =>
												array (
													'name' => 'demo1',
													'title' => '功能演示一',
												),
											1 =>
												array (
													'name' => 'demo2',
													'title' => '功能演示二',
												),
										),
								),
							18 =>
								array (
									'name' => 'transfer',
									'title' => '穿梭框',
								),
							19 =>
								array (
									'name' => 'tree',
									'title' => '树形组件',
								),
							20 =>
								array (
									'name' => 'colorpicker',
									'title' => '颜色选择器',
								),
							21 =>
								array (
									'name' => 'slider',
									'title' => '滑块组件',
								),
							22 =>
								array (
									'name' => 'rate',
									'title' => '评分',
								),
							23 =>
								array (
									'name' => 'carousel',
									'title' => '轮播',
								),
							24 =>
								array (
									'name' => 'flow',
									'title' => '流加载',
								),
							25 =>
								array (
									'name' => 'util',
									'title' => '工具',
								),
							26 =>
								array (
									'name' => 'code',
									'title' => '代码修饰',
								),
						),
				),
				array (
					'name' => 'template',
					'title' => '页面',
					'icon' => 'layui-icon-template',
					'list' =>
						array (
							0 =>
								array (
									'name' => 'personalpage',
									'title' => '个人主页',
									'jump' => 'template/personalpage',
								),
							1 =>
								array (
									'name' => 'addresslist',
									'title' => '通讯录',
									'jump' => 'template/addresslist',
								),
							2 =>
								array (
									'name' => 'caller',
									'title' => '客户列表',
									'jump' => 'template/caller',
								),
							3 =>
								array (
									'name' => 'goodslist',
									'title' => '商品列表',
									'jump' => 'template/goodslist',
								),
							4 =>
								array (
									'name' => 'msgboard',
									'title' => '留言板',
									'jump' => 'template/msgboard',
								),
							5 =>
								array (
									'name' => 'search',
									'title' => '搜索结果',
									'jump' => 'template/search',
								),
							6 =>
								array (
									'name' => 'reg',
									'title' => '注册',
									'jump' => 'user/reg',
								),
							7 =>
								array (
									'name' => 'login',
									'title' => '登入',
									'jump' => 'user/login',
								),
							8 =>
								array (
									'name' => 'forget',
									'title' => '忘记密码',
									'jump' => 'user/forget',
								),
							9 =>
								array (
									'name' => '404',
									'title' => '404 页',
									'jump' => 'template/tips/404',
								),
							10 =>
								array (
									'name' => 'error',
									'title' => '出错页',
									'jump' => 'template/tips/error',
								),
							11 =>
								array (
									'name' => '',
									'title' => '内嵌页面',
									'spread' => true,
									'list' =>
										array (
											0 =>
												array (
													'name' => '',
													'title' => 'iframe - Bing',
													'jump' => '/iframe/link/demo',
												),
										),
								),
						),
				),
				array (
					'name' => 'app',
					'title' => '应用',
					'icon' => 'layui-icon-app',
					'list' =>
						array (
							0 =>
								array (
									'name' => 'content',
									'title' => '内容系统',
									'list' =>
										array (
											0 =>
												array (
													'name' => 'list',
													'title' => '文章列表',
												),
											1 =>
												array (
													'name' => 'tags',
													'title' => '分类管理',
												),
											2 =>
												array (
													'name' => 'comment',
													'title' => '评论管理',
												),
										),
								),
							1 =>
								array (
									'name' => 'forum',
									'title' => '社区系统',
									'list' =>
										array (
											0 =>
												array (
													'name' => 'list',
													'title' => '帖子列表',
												),
											1 =>
												array (
													'name' => 'replys',
													'title' => '回帖列表',
												),
										),
								),
							2 =>
								array (
									'name' => 'message',
									'title' => '消息中心',
								),
							3 =>
								array (
									'name' => 'workorder',
									'title' => '工单系统',
									'jump' => 'app/workorder/list',
								),
						),
				),
				array (
					'name' => 'senior',
					'title' => '高级',
					'icon' => 'layui-icon-senior',
					'list' =>
						array (
							0 =>
								array (
									'name' => 'im',
									'title' => 'WebIM UI',
								),
							1 =>
								array (
									'name' => 'echarts',
									'title' => 'Echarts',
									'list' =>
										array (
											0 =>
												array (
													'name' => 'line',
													'title' => '折线图',
												),
											1 =>
												array (
													'name' => 'bar',
													'title' => '柱状图',
												),
											2 =>
												array (
													'name' => 'map',
													'title' => '地图',
												),
										),
								),
						),
				),
				array (
					'name' => 'user',
					'title' => '用户',
					'icon' => 'layui-icon-user',
					'list' =>
						array (
							0 =>
								array (
									'name' => 'user',
									'title' => '网站用户',
									'jump' => 'user/user/list',
								),
							1 =>
								array (
									'name' => 'administrators-list',
									'title' => '后台管理员',
									'jump' => 'user/administrators/list',
								),
							2 =>
								array (
									'name' => 'administrators-rule',
									'title' => '角色管理',
									'jump' => 'user/administrators/role',
								),
						),
				),
				array (
					'name' => 'set',
					'title' => '设置',
					'icon' => 'layui-icon-set',
					'list' =>
						array (
							0 =>
								array (
									'name' => 'system',
									'title' => '系统设置',
									'spread' => true,
									'list' =>
										array (
											0 =>
												array (
													'name' => 'website',
													'title' => '网站设置',
												),
											1 =>
												array (
													'name' => 'email',
													'title' => '邮件服务',
												),
										),
								),
							1 =>
								array (
									'name' => 'user',
									'title' => '我的设置',
									'spread' => true,
									'list' =>
										array (
											0 =>
												array (
													'name' => 'info',
													'title' => '基本资料',
												),
											1 =>
												array (
													'name' => 'password',
													'title' => '修改密码',
												),
										),
								),
						),
				),
				array (
					'name' => 'get',
					'title' => '关于',
					'icon' => 'layui-icon-auz',
					'jump' => 'system/about',
				),

		];
		return $this->api($menu);
	}
	public function lang(){
		$app=$this->app;$lang=$app->make('lang');

		$lang->load($app->getAppPath() . 'lang/' . $lang->getLangSet() . '.php','getLoad');

		#print_r(app('lang')->get(null,[],'getLoad'));

		return $this->api(app('lang')->get(null,[],'getLoad'));
	}
	public function userinfo(){
		return $this->api(['username'=>token_session('user_nickname')]);
	}
	public function logout(){
		token_session('user_id',NULL);
		token_session('user_email',NULL);
		token_session('user_nickname',NULL);
		return $this->code(0);
	}
	public function login(){
		$request=app('request');
		$data=array();
		$data['email']=$request->post('email','','trim');
		$data['password']=$request->post('password','','trim');
		$data['vercode']=$request->post('vercode','','trim');

		//检查是否登陆
		if(!Captcha::check($data['vercode'])){
			$res=array('code'=>3,'msg'=>lang('the verification code is incorrect'));
		}elseif(!filter_var($data['email'], FILTER_VALIDATE_EMAIL)){
			$res=array('code'=>1,'msg'=>lang('please enter the correct email address'));
		}
		//登陆 设置session user_id user_email access_token
		else{
			if($this->doLogin($data)){
				$res=array('code'=>0,'msg'=>'','data'=>array('access_token'=>access_token()));
			}else{
				$res=array('code'=>2,'msg'=>lang('login failure'));
			}
		}
		return $this->result($res);
	}
	private function doLogin($data){
		$row =  Seller::where('email',$data['email'])->where('type',0)->find();
			//如果用户名 密码匹配,设置
		if(!is_null($row) && $row->password == md5(md5($data['password']) . $row->salt) ){
			token_session('user_id',$row->id);
			token_session('user_email',$row->email);
			token_session('user_nickname',$row->nickname);
				//更新
			$row->loginfailure = 0;
			$row->logintime = time();
			$row->loginip = $this->request->ip();
			$row->loginnum	=  Db::raw('loginnum+1');
			return $row->save();
		}
		return false;
	}
	public function reg(){
		$data=[];
		$data['email']=input('post.email','','trim');
		$data['password']=$_POST['password'];
		$data['nickname']=input('post.nickname','','trim');
		$data['parentid']=input('post.parentid',0,'intval');
		$agree=input('post.agree','off','trim');

		$msg='';
		if(config('user.seller_reg_close')=='是'){
			$msg='本站已关闭注册';
		}elseif(session('email')!=$data['email'] || session('email_verify')!=input('post.vercode','','trim')){
				$msg=lang('the verification code is incorrect');
		}elseif(!Db::name('user')->field('id')->find($data['parentid'])){
			$msg=lang('incorrect invitation code');
		}elseif($agree!=='on'){
			$msg=lang('you must agree to the user agreement to register');
		}

		if($msg)
			return $this->code(1,$msg);


		Db::startTrans();
		try {
			$validate = new \app\seller\validate\Seller;
			if(!$validate->scene('add')->check($data)){
				$result=['code'=>1,'msg'=>$validate->getError()];// 验证失败 输出错误信息
			}
			else{

				$data['salt'] = substr(md5(microtime(true)),0,6);
				$data['password'] = md5(md5($data['password']) . $data['salt']);
				$data['joinip'] = $this->request->ip();

				(new Seller)->save($data);
				//验证码失效
				session('email',NULL);
				session('email_verify',NULL);
				$result=['code'=>0,'msg'=>lang('successful registration')];

			}
			Db::commit();
		} catch (\Exception $e) {
			Db::rollback();
			$result=['code'=>2,'msg'=>$e->getMessage()];

		}
		return $this->result($result);
	}


}
